import Vue from 'vue';
import VueRouter from 'vue-router';
import store from "../store"

Vue.use(VueRouter)

//验证某个路由的登录权限
function fn(path, next) {
  if (store.state.user.menus_url.includes(path)) {
    next()
  } else {
    next('/index')
  }
}
//导出路由
export const IndexRoutes = [{
    meta: {
      title: "菜单管理"
    },
    path: "menu",
    component: () => import("../pages/menu/menu.vue"),
    beforeEnter(to, from, next) {
      fn("/menu", next)
    }
  },
  {
    meta: {
      title: "角色管理"
    },
    path: "role",
    component: () => import("../pages/role/role.vue"),
    beforeEnter(to, from, next) {
      fn("/role", next)
    }
  },
  {
    meta: {
      title: "管理员管理"
    },
    path: "manage",
    component: () => import("../pages/manage/manage.vue"),
    beforeEnter(to, from, next) {
      fn("/manage", next)
    }
  },
  {
    meta: {
      title: "商品分类"
    },
    path: "cate",
    component: () => import("../pages/cate/cate.vue"),
    beforeEnter(to, from, next) {
      fn("/cate", next)
    }
  },
  {
    meta: {
      title: "商品规格"
    },
    path: "specs",
    component: () => import("../pages/specs/specs.vue"),
    beforeEnter(to, from, next) {
      fn("/specs", next)
    }
  },
  {
    meta: {
      title: "商品管理"
    },
    path: "goods",
    component: () => import("../pages/goods/goods.vue"),
    beforeEnter(to, from, next) {
      fn("/goods", next)
    }
  },
  {
    meta: {
      title: "会员管理"
    },
    path: "vip",
    component: () => import("../pages/vip/vip.vue"),
    beforeEnter(to, from, next) {
      fn("/vip", next)
    }
  },
  {
    meta: {
      title: "轮播图管理"
    },
    path: "banner",
    component: () => import("../pages/banner/banner.vue"),
    beforeEnter(to, from, next) {
      fn("/banner", next)
    }
  },
  {
    meta: {
      title: "秒杀活动"
    },
    path: "seckill",
    component: () => import("../pages/seckill/seckill"),
    beforeEnter(to, from, next) {
      fn("/seckill", next)
    }
  }
]
const routes = [{
    path: "/login",
    component: () => import("../pages/login/login.vue")
  },
  {
    path: "/index",
    component: () => import("../pages/index/index.vue"),
    children: [{
        path: "",
        component: () => import("../pages/home/home.vue")
      },
      ...IndexRoutes
    ]
  },
  {
    path: "*",
    redirect: "/login"
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === "/login") {
    next()
    return;
  }
  // console.log(store);
  if (store.state.user.token) {
    next()
    return;
  }
  next("/login")
})
export default router